package com.smartens.boit.login;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartens.boit.MainActivity;
import com.smartens.boit.R;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.LoginBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 登录
 * @Author songtt
 * @Date 2021/3/4
 */
public class LoginActivity extends AppCompatActivity {
    private TextView textView;
    private HttpHelper helper;
    private EditText name;
    private EditText pwd;
    private ImageView delName;
    private ImageView delPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        textView = findViewById(R.id.login);
        name = findViewById(R.id.ed_name);
        pwd = findViewById(R.id.ed_pwd);
        delName = findViewById(R.id.del_name);
        delPwd = findViewById(R.id.del_pwd);
        helper = HttpHelper.getInstance();

        click();

    }

    @Override
    protected void onStart() {
        super.onStart();
        // 设置输入框光标
        if (!TextUtils.isEmpty(name.getText().toString())) {
            name.setSelection(name.getText().length());
        }
    }

    public void click() {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getLogin();
            }
        });
        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                 Log.v("===shuru", s.toString());
                if (s.toString().length() > 0) {
                    delName.setVisibility(View.VISIBLE);
                } else {
                    delName.setVisibility(View.GONE);
                }
            }
        });

        delName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name.setText("");
                delName.setVisibility(View.GONE);
            }
        });
        pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    delPwd.setVisibility(View.VISIBLE);
                } else {
                    delPwd.setVisibility(View.GONE);
                }
            }
        });

        delPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwd.setText("");
                delPwd.setVisibility(View.GONE);
            }
        });
    }
 /*   public void getLogin() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        LoginActivity.this.finish();
    }*/
    public void getLogin() {

        if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(pwd.getText().toString())) {
            Toast.makeText(LoginActivity.this, "请输入", Toast.LENGTH_SHORT).show();
        } else {
            helper.login(name.getText().toString(), pwd.getText().toString(), new HttpHelper.HttpCallBack<LoginBean>() {

                @Override
                public void onSuccess(List<LoginBean> result) {
                    setStore();
                     Log.v("=====onSuccess", result.get(0).getUserName());
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                }

                @Override
                public void onFailure(final String message) {
                     Log.v("=====onFailure", message);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this,  message, Toast.LENGTH_LONG).show();
                        }
                    });
//

                }
            });
        }
    }

    public void setStore() {
        //步骤1：创建一个SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        //步骤2： 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //步骤3：将获取过来的值放入文件
        editor.putString("userName", name.getText().toString());
        editor.putString("password", pwd.getText().toString());
        //步骤4：提交
        editor.apply();

    }


}